/**
 */
package com.dingpeikeji.quickdeer.core.modules.distribution.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.BaseService;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.common.utils.SpringContextHolder;
import com.dingpeikeji.quickdeer.core.modules.cupons.dao.TCuponsDao;
import com.dingpeikeji.quickdeer.core.modules.cupons.entity.CuponsInfo;
import com.dingpeikeji.quickdeer.core.modules.distribution.dao.TDistributionDao;
import com.dingpeikeji.quickdeer.core.modules.distribution.entity.DistributionInfo;
import com.dingpeikeji.quickdeer.core.modules.line.dao.TLineDao;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.site.dao.TSitedtlDao;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.mobile.dao.OrderDao;
import com.dingpeikeji.quickdeer.mobile.entity.OrderInfo;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;

/**
 * 分销Service
 * 
 */
@Service
@Transactional(readOnly = true)
public class TDistributionService extends CrudService<TDistributionDao, DistributionInfo> {
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	@Autowired
	private TDistributionDao tDistributionDao;
	@Autowired
	private OrderDao orderDao; 
	
	public Integer updateById(DistributionInfo distributionInfo){
		distributionInfo.setWithdrawTime(new Date());
		return tDistributionDao.updateById(distributionInfo);
	}
	
	public List<DistributionInfo> getByUserId(String userId){
		return tDistributionDao.getByUserId(userId);
	}
	
	public Integer insertWhitdraw(DistributionInfo distributionInfo) throws Exception{
		distributionInfo.setCreateTime(new Date());
		distributionInfo.setDistributionId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
		distributionInfo.setStatus("0");
		List<OrderInfo> info =orderDao.getNoBalanceList(distributionInfo.getUserId());
		long time =ValidateTool.getMonthFirstDay(df.format(new Date()));//获取指定月的第一天
		int j=0;
		for(int i=0;i<info.size();i++){
			if(info.get(i).getCreateDate().getTime()<time){
				 j+=orderDao.updateOrderBalance(info.get(i).getOrderId(), "2");
			}
		}
		if(j>0){
			Integer insertWhitdraw = tDistributionDao.insertWhitdraw(distributionInfo);
			return insertWhitdraw;
		}else{
			return 0;
		}
	}
	
	//获取提现利率
	public String getRate(){
		return tDistributionDao.getRate();
	}
	
	//更新提现利率
	public Integer updateRate(String rate){
		return tDistributionDao.updateRate(rate);
	}

	//插入订单结算记录
	public Integer insertWithdrawSettle(DistributionInfo distributionInfo){
		return tDistributionDao.insertWithdrawSettle(distributionInfo);
	}
	
	//根据记录id查找结算订单信息
	public List<OrderInfo> getWithdrawSettleById(String distributionId){
		return tDistributionDao.getWithdrawSettleById(distributionId);
	}
}
